package wifiunion.com.notice.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import wifiunion.com.notice.Constants;
import wifiunion.com.notice.R;
import wifiunion.com.notice.entity.NoticeTopGroupEntity;
import wifiunion.com.notice.entity.SubNoticeTargetEntity;


/**
 * 作 者: 
 * 创建时间: 2013-12-9 上午11:05:35<br>
 * 版权声明: Copyright (C) 2006-2013 <br>
 * 修改历史: 2013-12-9 1.0.0 初始版本<br>
 * 文件描述: 查看报告
 */
public class NoticeTargetAdapter extends BaseExpandableListAdapter {


	private Context mContext;

    private ArrayList<NoticeTopGroupEntity> flist  = new ArrayList<NoticeTopGroupEntity>();
	private View.OnClickListener onClickListener;

	public NoticeTargetAdapter(Context mContext, ArrayList<NoticeTopGroupEntity> flist,View.OnClickListener listener) {
		super();
		this.mContext = mContext;
		this.flist = flist;
		this.onClickListener = listener;
		
	}

	public void setData(ArrayList<NoticeTopGroupEntity> flist) {
		this.flist = flist;
	}

	@Override
	public int getGroupCount() {
		if (null != flist && flist.size() >0) {
			return flist.size();
		} else {
			return 0;
		}

	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if (null != flist && null != flist.get(groupPosition)
				&& null != flist.get(groupPosition).getSubNoticeTargetList()) {
			return flist.get(groupPosition).getSubNoticeTargetList().size() + 1;
		} else {
			return 1;
		}
		
	}

	@Override
	public Object getGroup(int groupPosition) {
		if(null != flist && flist.size() >0 )
		{
			return flist.get(groupPosition);
		}
		else
		{
			return 0;
		}
//		return null;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return flist.get(groupPosition).getSubNoticeTargetList().get(childPosition);
		
//		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// 实例化布局文件
		RelativeLayout glayout = (RelativeLayout) LayoutInflater.from(mContext)
				.inflate(R.layout.parent_noticetarget_item, null);

		ImageView stateIv = (ImageView) glayout.findViewById(R.id.pater_state_iv);
		TextView  nameTv  = (TextView) glayout.findViewById(R.id.pater_name_tv);

		NoticeTopGroupEntity gp  = (NoticeTopGroupEntity)flist.get(groupPosition);
		nameTv.setText(gp.getGroupName());

		if (isExpanded) {
			stateIv.setImageResource(R.drawable.open);

		} else {
			stateIv.setImageResource(R.drawable.close);

		}

		return glayout;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// 实例化布局文件
		RelativeLayout clayout = (RelativeLayout) LayoutInflater.from(mContext)
				.inflate(R.layout.child_noticetarget_item, null);

		ImageView headIv = (ImageView) clayout.findViewById(R.id.child_head_iv);
		TextView name = (TextView) clayout.findViewById(R.id.chil_member_name);
		TextView optItem = (TextView) clayout.findViewById(R.id.child_optitem_name);
		View itemline = (View) clayout.findViewById(R.id.item_line_vv);
		optItem.setTag(groupPosition);
		optItem.setOnClickListener(onClickListener);
		RelativeLayout dataRl = (RelativeLayout) clayout.findViewById(R.id.child_data_rl);

		if(childPosition ==0)
		{
			optItem.setVisibility(View.VISIBLE);
			dataRl.setVisibility(View.GONE);
			itemline.setVisibility(View.GONE);
		}else
		{
			optItem.setVisibility(View.GONE);
			dataRl.setVisibility(View.VISIBLE);
			itemline.setVisibility(View.VISIBLE);
			SubNoticeTargetEntity md  = (SubNoticeTargetEntity)flist.get(groupPosition).getSubNoticeTargetList().get(childPosition -1);
			name.setText(md.getShowName1()+"("+md.getShowName2()+")");
			Glide.with(mContext).load(Constants.HTTP_URL_IMAGESERVER + md.getPicUrl()).placeholder(R.drawable.device_location)
					.error(R.drawable.device_location).fitCenter().into(headIv);

		}
//		if(md.isChecked())
//		{
//			choooseIv.setSelected(true);
//		}
//		else
//		{
//			choooseIv.setSelected(false);
//		}


		return clayout;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}
